<?php
/*
* Author: Jason ZHENG <zmj522008@hotmil.com>
* Date  : 2018/05/23
* Class : Message 消息/站内信
*/
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Session;
use think\Request;

class Message extends Controller
{ 
	public function index()
	{ 
		echo 'Message Controller'; 
	}

	//创建一个消息
	public function create(Request $request)
	{
		$postData = $request->post();
		$rules = [
            "title|标题"		=> "require|max:50",
            "content|内容"	=> "require",
            "type|类型"		=> "require|number",
		];

		if($this->validate($postData, $rules) === true)
		{
			if(Db::table('message')->insert([
					'user_id'		=> Session::get('user.id'),
					'title' 		=> $postData['title'],
					'content'		=> $postData['content'],
					'type' 			=> $postData['type'],
					'create_time' 	=> date('Y:m:d H:i:s', time()),
				]))
				return ['code' => 666, 'msg' => 'ok', 'data' => null];
			else
				return ['code' => 10001, 'msg' => 'database error', 'data' => null];
		}else
		{
			return ['code' => 10010, 'msg' => $this->validate($postData, $rules), 'data' => null];
		}
	}

	// 获取消息类型
	public function types()
	{
		$types = Db::table('message_types')
				->where(['state' => 0])
				->field('label, value')
				->select();
		return ['code' => 666, 'msg' => 'ok', 'data' => $types];
	}

	// 获取消息列表
	public function lists($page=1)
	{
		$all = Db::table('message')
				->alias('m')
				->join('message_types mt', 'm.type = mt.label', 'LEFT')
				->where(['m.is_delete' => 0 ])
				->field('m.*, mt.value type_value')
				->select();

		$count = Db::table('message')
				->where(['is_delete' => 0 ])
				->count();
		
		$unRead = $count;
		foreach ($all as $key => $message) 
		{
			$message_log = Db::table('message_log')
							->where(['message_id' => $message['id'], 'user_id' => Session::get('user.id') ])
							->find();
			if(empty($message_log)){
				$all[$key] = array_merge($all[$key], ['is_read' => false]);
			}else{
				$all[$key] = array_merge($all[$key], ['is_read' => true]);
				$unRead --;
			}
		}

		$lists = array_slice($all, ($page-1)*10, 10);

		return ['code' => 666, 'msg' => 'feedback lists', 'data' => ['count'=> $count, 'unRead' => $unRead, 'lists' => $lists]];
	}

	// 删除一条消息
	public function delete(Request $request)
	{
		$postData = $request->post();
		$rules = [
            "id|id"	=> "require|number",
		];

		if($this->validate($postData, $rules) === true)
		{
			if(! empty(
				Db::table('message')
					->where([
							'user_id'		=> Session::get('user.id'),
							'id' 			=> $postData['id'],
							'is_delete'     => 0,
							])
					->find()
				)){

				Db::table('message')
					->where([
							'user_id'		=> Session::get('user.id'),
							'id' 			=> $postData['id'],
							])
					->update(['is_delete' => 1]);
				return ['code' => 666, 'msg' => '删除成功', 'data' => null];
			}
				
			else
				return ['code' => 10001, 'msg' => '消息不存在或已删除', 'data' => null];
		}else
		{
			return ['code' => 10010, 'msg' => $this->validate($postData, $rules), 'data' => null];
		}
	}

	// 获取一条消息
	public function get(Request $request)
	{
		$postData = $request->post();
		$rules = [
            "id|id"	=> "require|number",
		];

		if($this->validate($postData, $rules) === true)
		{
			if(! empty(
				Db::table('message')
					->where([
							'id' 			=> $postData['id'],
							'is_delete'     => 0,
							])
					->find()
				)){

				$message = Db::table('message')
					->alias('m')
					->join('message_types mt', 'm.type = mt.label', 'LEFT' )
					->where([
							'm.id' 			  => $postData['id'],
							'm.is_delete'     => 0,
							])
					->field('m.*, mt.value type_value')
					->find();

				$message_log = Db::table('message_log')
								->where(['user_id' => Session::get('user.id'), 'message_id' => $message['id']])
								->find();
				if(empty($message_log))
				{
					Db::table('message_log')
								->insert([
											'user_id' => Session::get('user.id'),
											'message_id' => $message['id'],
											'create_time'	=> Date('Y-m-d H:i:s',time()),
										]);
				}
				return ['code' => 666, 'msg' => 'ok', 'data' => $message];
			}
				
			else
				return ['code' => 10001, 'msg' => '消息不存在或已删除', 'data' => null];
		}else
		{
			return ['code' => 10010, 'msg' => $this->validate($postData, $rules), 'data' => null];
		}
	}

}